﻿using System.Linq;
using System.Web.Mvc;
using NOSDC.Areas.Admin.Models;
using NOSDC.Repositories;

namespace NOSDC.Areas.Admin.Controllers
{
    public class SpeakerController : BaseController
    {
        readonly ISpeakerRepository _repository;

        public SpeakerController() 
            : this(new SpeakerRepository()) { }

        public SpeakerController(ISpeakerRepository repository)
        {
            _repository = repository;
        }

        // GET: /Admin/Speaker/
        public ActionResult Index()
        {
            var speakerModel = new SpeakerViewModel();
            speakerModel.Speaker = _repository.GetAll().ToList();
            return View(speakerModel);
        }

        public ActionResult Create()
        {
            var model = new SpeakerCreateViewModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(SpeakerCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                _repository.Add(model.Speaker);
                return RedirectToAction("Index");
            }

            return View(model);
        }

        public ActionResult Update(int id)
        {
            var model = new SpeakerCreateViewModel();
            model.Speaker = _repository.GetById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Update(SpeakerCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                _repository.Update(model.Speaker);
                return RedirectToAction("Index");
            }

            return View(model);
        }
    }
}
